package com.chf.job.service.impl;

import com.chf.job.dao.UserMapper;
import com.chf.job.pojo.User;
import com.chf.job.service.UserService;
import com.chf.job.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 用户业务层
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 校验邮箱是否存在
     * @param email
     */
    @Override
    public boolean verifyEmail(String email) {
        String userEmail = userMapper.selectByEmail(email);
        if (StringUtils.isEmpty(userEmail)) {
            return true;
        }
        return false;
    }

    /**
     * 添加用户信息
     * @param email
     * @param password
     * @param type
     * @return
     */
    @Override
    public int addUser(String email, String password, Integer type) {
        User user = new User();
        user.setEmail(email);
        user.setPassword(MD5Util.MD5EncodeUtf8(password));
        user.setType(type);
        return userMapper.insertSelective(user);
    }

    /**
     * 用户登录
     * @param email
     * @param password
     * @return
     */
    @Override
    public User login(String email, String password) {
        return userMapper.selectByEmailAndPassword(email, MD5Util.MD5EncodeUtf8(password));
    }

    /**
     * 用户未登录情况下找回密码
     * @param email
     * @return
     */
    @Override
    public int checkPwd(String email) {
        return userMapper.checkPwd(email);
    }

    /**
     * 更新密码
     * @param newPwd
     * @return
     */
    @Override
    public int updatePwd(String email, String newPwd) {
        return userMapper.updateByEmail(email, MD5Util.MD5EncodeUtf8(newPwd));
    }

    /**
     * 校验用户名和密码是否正确
     * @param email
     * @param oldPwd
     * @return
     */
    @Override
    public int findPwd(String email, String oldPwd) {
        return userMapper.finPwd(email, MD5Util.MD5EncodeUtf8(oldPwd));
    }

    /**
     * 去数据库查询用户基本信息
     * @param email
     * @return
     */
    @Override
    public User getUserBaseInfo(String email) {
        return userMapper.getUserBaseInfo(email);
    }

    /**
     * 更新真实姓名
     * @param email
     * @param userName
     */
    @Override
    public void updateUserName(String email, String userName) {
        userMapper.updateUserName(email, userName);
    }


}
